import { Ast, Visitor, createRootAst } from "./ast";
import { BaseVisitor } from "./core";
import { httpPost } from "./utils";
export interface InfoContext {
    [key: string]: any;
}

export class InfoVisitor implements Visitor<InfoContext>{
    visitNode(node: Ast, ctx: InfoContext): void {
        if (!node.visibleToUser) return;
        if (node.className) {
            const classNameSplit = node.className.split('.')
            const classNameEnd = classNameSplit.pop()
            const methodName = `visit${classNameEnd.charAt(0).toUpperCase() + classNameEnd.slice(1)}`
            if (this[methodName]) return this[methodName](node, ctx)
        }
        node.children.map(c => c.visit(this, ctx))
    }
}

/**
 * 
 */
export interface VideoInfoContext extends InfoContext{
    name?: string;
    label?: string;
    live?: string;
    type?: string;
    like?: ()=>void;
}
export class VideoInfoVisitor extends InfoVisitor {
    visitTextView(node: Ast, ctx: VideoInfoContext) {
        if (node.id.endsWith(`user_name_text_view`)) {
            ctx.name = node.text;
        }
        if (node.id.endsWith(`user_name_text_view_new`)) {
            ctx.name = node.text;
        }
        if (node.id.endsWith(`element_caption_label`)) {
            ctx.label = node.text;
        }
        if (node.id.endsWith(`live_preview_bottom_caption`)) {
            ctx.label = node.text;
        }
        if (node.id.endsWith(`live_cover_icon_text`)) {
            ctx.live = node.text;
        }
        if (node.id.endsWith(`live_simple_play_swipe_text`) && node.clickable) {
            ctx.type = `直播`
            ctx.toLive = () => node.click()
        }
        if (node.text === `上滑继续观看视频`) {
            // 调查
            ctx.type = `调查`
        }
    }
    visitFrameLayout(node: Ast, ctx: InfoContext) {
        if (node.id.endsWith(`like_button`) && node.clickable && node.visibleToUser) {
            ctx.like = () => node.click()
            return;
        }
        node.children.map(c => c.visit(this, ctx))
    }
    visitViewGroup(node: Ast, ctx: InfoContext) {
        if (node.desc === `action_bar_view`) {
            ctx.type = `推广`
            ctx.action_bar = node.children.map(c => c.text).join(' ')
        }
        if (node.desc === `close_view` && node.clickable && node.visibleToUser) {
            ctx.close = () => node.click()
            ctx.type = `广告`
        }
    }
}


export function getVideoInfo(): VideoInfoContext {
    const ctx: InfoContext = {}
    const root = createRootAst()
    root.visit(new VideoInfoVisitor(), ctx)
    return ctx;
}


interface ProfileInfo {
    fans_count?: number;
    follow_count?: number;
    like_count?: number;
    user_name?: string;
    ksid?: string;
    desc?: string;
    follow?: () => void;
}


export class ProfileInfoVisitor extends BaseVisitor {
    visitViewGroup(node: Ast, ctx: ProfileInfo) {
        if (node.id.endsWith(`follower_layout`)) {
            const res = node.desc.match(/(\d+)粉丝/)
            if (res && res.length > 0) {
                const [, count] = res;
                ctx.fans_count = parseInt(count);
            }
        }
        if (node.id.endsWith(`following_layout`)) {
            const res = node.desc.match(/(\d+)关注/)
            if (res && res.length > 0) {
                const [, count] = res;
                ctx.follow_count = parseInt(count);
            }
        }

        if (node.id.endsWith(`like_layout`)) {
            const res = node.desc.match(/(\d+)获赞/)
            if (res && res.length > 0) {
                const [, count] = res;
                ctx.like_count = parseInt(count);
            }
        }
    }

    visitTextView(node: Ast, ctx: ProfileInfo) {
        if (node.id.endsWith(`user_name_tv`)) {
            ctx.user_name = node.text.trim()
        }
        if (node.id.endsWith(`profile_user_kwai_id`)) {
            const res = node.desc.match(/快手号：(.*)/)
            if (res && res.length > 0) {
                const [, username] = res;
                ctx.ksid = username.trim();
            }
        }
    }

    visitToggleButton(node: Ast, ctx: ProfileInfo) {
        if (node.id.endsWith(`header_follow_button`)) {
            ctx.follow = () => node.click()
        }
    }

    visitScrollView(node: Ast, ctx: ProfileInfo) {
        if (node.id.endsWith(`user_text_wrapper`)) {
            const texts = node.textTexts()
            ctx.desc = texts;
        }
    }
}

export function getProfileInfo() {
    const root = createRootAst()
    const visitor = new ProfileInfoVisitor()
    const ctx: ProfileInfo = {}
    visitor.visitNode(root, ctx)
    return ctx;
}


export function getProfileInfoAndUpload() {
    const res = httpPost('cj', {
        type: '口令'
    });
    const userId = res.user_id;
    toProfilePage(userId)
    sleep(3000)
    const ctx = getProfileInfo()
    httpPost(`cjSave`, {
        fans_count: ctx.fans_count,
        follow_count: ctx.follow_count,
        like_count: ctx.like_count,
        user_name: ctx.user_name,
        ksid: ctx.ksid,
        desc: ctx.desc,
        userId: userId,
        has_live: false
    })
    sleep(3000)
}


function toProfilePage(id: string) {
    const uri = `kwai://profile/${id}`;
    const intent = app.intent({
        action: "android.intent.action.VIEW",
        data: uri
    });
    context.startActivity(intent);
}